<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Description of fornecedor_model
 *
 * @author Giovani
 */
class FornecedorModel extends CI_Model {

    function __construct(){
        parent::__construct();
    }

    public function inserir($dados){
        $this->db->insert("fornecedor", $dados);
        return $this->db->insert_id();
    }

    public function update($id, $dados){
        $this->db->where("idFornecedor", $id);
        $this->db->update("fornecedor", $dados);
    }

    public function delete($id){
        $this->db->where("idFornecedor", $id);
        $this->db->delete("fornecedor");
    }

    public function getFornecedor($id){
        $this->db->from("fornecedor");
        $this->db->where("idFornecedor", $id);
        $query = $this->db->get();

        if( $query->num_rows() > 0 ) :
            return $query->row(0);

        else :
            return false;

        endif;
    }

    public function listar(){
        $query = $this->db->get("fornecedor");
        return $query;
    }

    public function getFornecedorEmail($email){
        $this->db->where("email", $email);
        $this->db->from("fornecedor");
        $query = $this->db->get();

        if( $query->num_rows() > 0 ) :
            return $query->row(0);
        
        else :
            return false;

        endif;
    }
    public function getFornecedorLink($str){
        $vetor = explode(".",$str);
        if(count($vetor > 1)){
        $this->db->where("nome", $vetor[0]);
        $this->db->where("sobrenome", $vetor[1]);
        }
        else
            $this->db->where("razaoSocial", $vetor[0]);

        $this->db->from("fornecedor");
        $query = $this->db->get();

        if( $query->num_rows() > 0 ) :
            return $query->row(0);

        else :
            return false;

        endif;

    }
    
    public function buscar($str){
        $this->db->from("fornecedor");        
        $this->db->like("razaoSocial", $str);
        $this->db->where("autorizado", true);
        $query = $this->db->get();
        if( $query->num_rows() > 0 ) 
            return $query;
        else 
            return false;
    }

    public function getFornecedorCnpj($cnpj){
        $this->db->where("cnpj", $cnpj);
        $this->db->from("fornecedor");
        $query = $this->db->get();

        if( $query->num_rows() > 0 ) :
            return $query->row(0);

        else :
            return false;

        endif;
    }
}
